package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.ClazzService;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Autowired
    private ClazzService clazzService;
    @Autowired
    private StudentMapper studentMapper;

    /**
     * 分页条件查询学生
     *
     * @param student
     * @return
     */
    @Override
    public PageResult<Student> getStudentBypage(StudentQueryParam student) {
        Page<Student> page = Page.of(student.getPage(), student.getPageSize());
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(student.getName() != null && !student.getName().isEmpty(), Student::getName, student.getName())
                .eq(student.getDegree() != null, Student::getDegree, student.getDegree())
                .eq(student.getClazzId() != null, Student::getClazzId, student.getClazzId());
        page = page(page, queryWrapper);
        List<Student> studentList = page.getRecords();
        List<Integer> classIds = studentList.stream().map(stu -> stu.getClazzId()).collect(Collectors.toList());
        if (classIds != null && !classIds.isEmpty()) {
            List<Clazz> clazzList = clazzService.listByIds(classIds);
            studentList.forEach(stu -> {
                clazzList.forEach(clazz -> {
                    if (stu.getClazzId() != null && stu.getClazzId() == clazz.getId()) {
                        stu.setClazzName(clazz.getName());
                    }
                });
            });
        }
        PageResult<Student> pageResult = new PageResult<>(page.getTotal(), studentList);
        return pageResult;
    }

    /**
     * 根据ID查询学生
     *
     * @param id
     * @return
     */
    @Override
    public Student getStudentById(Integer id) {
        Student student = getById(id);
        Integer clazzId = student.getClazzId();
        Clazz clazz = clazzService.getById(id);
        String clazzName = clazz.getName();
        student.setClazzName(clazzName);
        return student;
    }

    /**
     * 违纪扣分
     *
     * @param id
     * @param score
     */
    @Override
    public void violation(Integer id, Integer score) {
        Student student = getById(id);
        student.setId(id);
        short ViolationCount = (short)(student.getViolationCount() + 1);
        short ViolationScore = (short)(student.getViolationScore() + score);
        student.setViolationCount(Short.valueOf(ViolationCount));
        student.setViolationScore(Short.valueOf(ViolationScore));
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.updateById(student);
    }
}
